package com.example.springboot.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.springboot.common.HoneyLogs;
import com.example.springboot.common.LogType;
import com.example.springboot.common.Result;
import com.example.springboot.entity.Equipment;
import com.example.springboot.entity.User;
import com.example.springboot.exception.ServiceException;
import com.example.springboot.service.EquipmentService;
import com.example.springboot.service.UserService;
import com.example.springboot.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 功能：操作洗衣机设备的增删改查
 * 作者：zhoujun
 * 日期： 2024/5/2 16:12
 */

@CrossOrigin
@RestController
@RequestMapping("/equipment")
public class EquipmentController {
    @Autowired
    EquipmentService equipmentService;
    @HoneyLogs(operation = "设备", type = LogType.ADD)
    @PostMapping("/add")
    public Result add(@RequestBody Equipment equipment){
        User currentUser = TokenUtils.getCurrentUser();  // 获取到当前登录的用户信息

        equipment.setDevtime(DateUtil.now());
        equipment.setRole(currentUser.getUsername());
        equipmentService.save(equipment);
        return Result.success();
    }

    @HoneyLogs(operation = "设备", type = LogType.UPDATE)
    @PutMapping("/update")
    public Result update(@RequestBody Equipment equipment){
        equipmentService.updateById(equipment);
        return Result.success();
    }

    @HoneyLogs(operation = "设备", type = LogType.DELETE)
    @DeleteMapping("/delete/{devId}")
    public Result delete(@PathVariable Integer devId){

       equipmentService.removeById(devId);
        return Result.success();
    }

    @HoneyLogs(operation = "设备", type = LogType.BATCH_DELETE)
    @DeleteMapping("/delete/batch")
    public Result batchDelete(@RequestBody List<Integer> ids){

        equipmentService.removeBatchByIds(ids);
        return Result.success();
    }

    @GetMapping("/selectAll")
    public Result SelectAll(){
        List<Equipment> equipmentList= equipmentService.list(new QueryWrapper<Equipment>().orderByDesc("devid"));
        return Result.success(equipmentList);
    }

    @GetMapping("/selectById/{devId}")
    public Result selectById(@PathVariable Integer devId){
        Equipment equipmentList = equipmentService.getById(devId);
        return Result.success(equipmentList);
    }


    /**
     * 多条件模糊查询用户信息
     * pageNum 当前的页码
     * pageSize 每页查询的个数
     */
    @GetMapping("/selectByPage")
    public Result selectByPage(@RequestParam Integer pageNum,
                               @RequestParam Integer pageSize,
                               @RequestParam String devName) {
        QueryWrapper<Equipment> queryWrapper = new QueryWrapper<Equipment>();
        queryWrapper.like(StrUtil.isNotBlank(devName),"devName",devName);

        Page<Equipment> page = equipmentService.page(new Page<>(pageNum, pageSize),queryWrapper);

        return Result.success(page);
    }
}